*     *****************************************************************   *;
*     *****************************************************************   *; 
*   Date:          December 27, 2021                               	      *;
*   Authors:       Lisa Dellmuth and Jonas Tallberg				          *; 
*   Purpose: 	   Analyses for book Legitimacy Politics: Elite  		  *;
*    Communication and Public Opinion in Global Governance (CUP, 2023)    *;
*     ****************************************************************    *;
*     ****************************************************************    *;


set more off

use Elitecom.dta, clear



* First, extract estimates for figures


preserve
reg conf t_in_p [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(inp.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
*non–integer degrees of freedom for unequal–variance t–tests 
*(Satterthwaite, 1946)[5], which metaparm uses as the default
restore

preserve
reg conf t_in_n [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(inn.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_out_p [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(outp.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_out_n [aw=weight], cluster(id)   
parmest, label norestore escal(N) saving(outn.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

***
preserve
reg conf t_cso_p [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(csop.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_cso_n [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(cson.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_io_p [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(iop.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_io_n [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(ion.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_gov_p [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(govp.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_gov_n [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(govn.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore


***

preserve
reg conf t_p [aw=weight], cluster(id) 
parmest, label norestore escal(N) saving(pos.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore

preserve
reg conf t_n [aw=weight], cluster(id)  
parmest, label norestore escal(N) saving(neg.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 


***


preserve
reg conf t_cso_io_p   [aw=weight],  cluster(id)
eststo csoiop
parmest, label norestore escal(N) saving(csoiop.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

preserve
reg conf t_cso_io_n  [aw=weight],  cluster(id)
eststo csoion
parmest, label norestore escal(N) saving(csoion.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

preserve
reg conf t_gov_io_p  [aw=weight],  cluster(id)
eststo goviop
parmest, label norestore escal(N) saving(goviop.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

preserve
reg conf t_gov_io_n  [aw=weight],  cluster(id)
eststo govion
parmest, label norestore escal(N) saving(govion.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

preserve
reg conf t_cso_gov_p  [aw=weight],  cluster(id)
eststo csogovp
parmest, label norestore escal(N) saving(csogovp.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

preserve
reg conf t_cso_gov_n  [aw=weight],  cluster(id)
eststo csogovn
parmest, label norestore escal(N) saving(csogovn.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 


***

preserve
reg conf t_in_out_p  [aw=weight], cluster(id)
eststo inoutp
parmest, label norestore escal(N) saving(inoutp.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

preserve
reg conf t_in_out_n  [aw=weight], cluster(id)
eststo inoutn
parmest, label norestore escal(N) saving(inoutn.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

***

preserve
reg conf t_pn [aw=weight], cluster(id)
eststo pn 
parmest, label norestore escal(N) saving(pn.dta, replace)
list parm estimate stderr dof min95 max95, clean noobs
restore 

***Merge data sets for Figures 1-3


use inp.dta, clear
merge 1:1 parm using inn.dta
drop _merge
merge 1:1 parm using outp.dta
drop _merge
merge 1:1 parm using outn.dta
drop _merge
merge 1:1 parm using csop.dta
drop _merge
merge 1:1 parm using cson.dta
drop _merge
merge 1:1 parm using iop.dta
drop _merge
merge 1:1 parm using ion.dta
drop _merge
merge 1:1 parm using govp.dta
drop _merge
merge 1:1 parm using govn.dta 
drop _merge
merge 1:1 parm using pos.dta
drop _merge
merge 1:1 parm using neg.dta 
drop _merge
merge 1:1 parm using csoiop.dta 
drop _merge
merge 1:1 parm using csoion.dta 
drop _merge
merge 1:1 parm using goviop.dta 
drop _merge
merge 1:1 parm using govion.dta 
drop _merge
merge 1:1 parm using csogovp.dta 
drop _merge
merge 1:1 parm using csogovn.dta 
drop _merge
merge 1:1 parm using inoutp.dta 
drop _merge
merge 1:1 parm using inoutn.dta 
drop _merge
merge 1:1 parm using pn.dta 
drop _merge
saveold fig1_3.dta, replace

ed

*Merge data in IO-specific datasets first
****************************************************************
use fig1_3.dta, clear
ed
drop in 1
drop label
gen io5=1

gen tg=.
label var tg "Treatment group"
sort tg 

gen ate=.
gen yl=.
gen yu=.

sort parm

save fig1_3.dta, replace


*Elite types - figure 1

use fig1_3.dta, clear
ed
sort parm

replace tg=1 if parm=="t_cso_p"
replace tg=2 if parm=="t_cso_n"
replace tg=3 if parm=="t_io_p"
replace tg=4 if parm=="t_io_n"
replace tg=5 if parm=="t_gov_p"
replace tg=6 if parm=="t_gov_n"
replace tg=7 if parm=="t_cso_io_p"
replace tg=8 if parm=="t_cso_io_n"
replace tg=9 if parm=="t_gov_io_p"
replace tg=10 if parm=="t_gov_io_n"
replace tg=11 if parm=="t_cso_gov_p"
replace tg=12 if parm=="t_cso_gov_n"

label define tg 1 "Positive-CSOs" 2 "Negative-CSOs" 3 "Positive-IOs" 4 "Negative-IOs" 5 "Positive-Governments" 6 "Negative-Governments" 7 "Positive-CSO-IO" 8 "Negative-CSO-IO" 9 "Positive-Gov-IO" 10 "Negative-Gov-IO" 11 "Positive-CSO-Gov" 12 "Negative-CSO-Gov"
label values tg tg

drop if tg==.
saveold fig1.dta, replace


*Object of framing - figure 2

use fig1_3.dta, clear
ed
sort parm

replace tg=1 if parm=="t_in_p"
replace tg=2 if parm=="t_in_n"
replace tg=3 if parm=="t_out_p"
replace tg=4 if parm=="t_out_n"
replace tg=5 if parm=="t_in_out_p"
replace tg=6 if parm=="t_in_out_n"

label define tg 1 "Positive-input" 2 "Negative-input" 3 "Positive-output" 4 "Negative-output" 5 "Positive-in-out" 6 "Negative-in-out"
label values tg tg

drop if tg==.
saveold fig2.dta, replace


*Valence - figure 3

use fig1_3.dta, clear
ed
sort parm

replace tg=1 if parm=="t_p"
replace tg=2 if parm=="t_n" 
replace tg=3 if parm=="t_pn" 

label define tg 1 "Positive" 2 "Negative" 3 "Negative-positive"
label values tg tg

drop if tg==.
saveold fig3.dta, replace


***Figures will look like this:

/*
		|
		|			io
		|
y=ATE	|
		|
		|
		|
		|
		-------------------
				x=tg
				

				*/
				
				

				
*Second, run code for Figures
*Fig 4.1
************************************************

use fig1.dta, clear

drop yl yu ate io5
				
rename min95 yl
rename max95 yu
rename estimate ate
			
local varname ate
local group1 tg 

sort `group1'
gen x = _n 
replace x = _n + 2 if _n > 6
tab x

twoway (scatter ate x , msymbol(S) msize(medium) mcolor(black)) ///
       (rcap yu yl x, lcolor(black))  ///
       , scheme(lean1)  ///
	   xlabel(none) xtitle(" ", size(3)) b2("Treatment groups compared to control group") ///
	   ytitle(Average treatment effect, size(3)) yscale(r(-1(.5)1)) ///
	   text(-1.2 1 "CSOs (pos.)", place(e) size(2.5)) ///
	   text(-1.2 2 "CSOs (neg.)", place(e) size(2.5)) ///
	   text(-1.2 3 "IOs (pos.)", place(e) size(2.5)) ///
	   text(-1.2 4 "IOs (neg.)", place(e) size(2.5)) ///
	   text(-1.2 5 "Governments (pos.)", place(e) size(2.5)) ///
	   text(-1.2 6 "Governments (neg.)", place(e) size(2.5)) ///
	   text(-1.2 9 "CSOs vs IOs (pos.)", place(e) size(2.5)) ///
	   text(-1.2 10 "CSOs vs IOs (neg.)", place(e) size(2.5)) ///
	   text(-1.2 11 "Governments vs IOs (pos.)", place(e) size(2.5)) ///
	   text(-1.2 12 "Governments vs IOs (neg.)", place(e) size(2.5)) ///
	   text(-1.2 13 "CSOs vs governments (pos.)", place(e) size(2.5)) ///
	   text(-1.2 14 "CSOs vs governments (neg.)", place(e) size(2.5)) ///
	   name(fig1, replace) graphregion(color(white)) ylab(, labsize(2.5) nogrid) ///
	   yline(0, lcolor(black) lpattern(dash))
		   
	   *Adjust x-title manually

	   

saveold fig1.dta, replace



*Fig 4.2
************************************************
use fig2.dta, clear

drop yl yu ate
				
rename min95 yl
rename max95 yu
rename estimate ate
			
local varname ate
local group1 tg 

sort `group1'  
gen x = _n
replace x = _n + 2 if _n >= 5 
tab x

twoway (scatter ate x , msymbol(S) msize(medium) mcolor(black)) ///
       (rcap yu yl x, lcolor(black))  ///
       , scheme(lean1)  ///
	   xlabel(none) xtitle(" ", size(3)) b2("Treatment groups compared to control group") ///
	   ytitle(Average treatment effect, size(3)) yscale(r(-.1(.5)1)) ///
	   text(-1.1 1 "Procedures (pos.)", place(e) size(2.5)) ///
	   text(-1.1 2 "Procedures (neg.)", place(e) size(2.5)) ///
	   text(-1.1 3 "Performance (pos.)", place(e) size(2.5)) ///
	   text(-1.1 4 "Performance (neg.)", place(e) size(2.5)) ///
	   text(-1.1 7 "Procedures vs performance (pos.)", place(e) size(2.5)) ///
	   text(-1.1 8 "Procedures vs performance (neg.)", place(e) size(2.5)) ///
	   name(fig2, replace) graphregion(color(white)) ylab(, labsize(2.5) nogrid) ///
	   yline(0, lcolor(black) lpattern(dash))
	   
	   *then manually adjust y-scale to -.75 at the lower boundary


saveold fig2.dta, replace


				
*Fig 4.3
************************************************
use fig3.dta, clear

drop yl yu ate
				
rename min95 yl
rename max95 yu
rename estimate ate
			
local varname ate
local group1 tg 

sort `group1' 
gen x = _n
replace x = _n + 2 if _n >= 3
tab x

twoway (scatter ate x , msymbol(S) msize(medium) mcolor(black)) ///
       (rcap yu yl x, lcolor(black))  /// 
        , scheme(lean1)  ///
	   xlabel(none) xtitle(" ", size(3)) b2("Treatment groups compared to control group") ///
	   ytitle(Average treatment effect, size(3)) yscale(r(-1(.5)1)) ///
	   text(-1.09 1 "Positive", place(e) size(2.5)) ///
	   text(-1.09 2 "Negative", place(e) size(2.5)) ///
	   text(-1.09 4 "Positive vs negative", place(e) size(2.5)) ///
	   name(fig3, replace) graphregion(color(white)) ylab(, labsize(2.5) nogrid) ///
	   yline(0, lcolor(black) lpattern(dash))
		   
	   *Adjust x-title manually


saveold fig3.dta, replace

exit




